/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.impl.restlet.resources;

import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.ResourceException;

import com.semagia.atomico.server.dm.impl.DefaultCollectionFeed;
import com.semagia.atomico.server.dm.impl.DefaultLinkAwareEntry;
import com.semagia.atomico.server.feed.impl.CollectionFeedWriter;
import com.semagia.atomico.server.storage.StorageException;

/**
 * Resource which provides a fragments entry and a snapshots entry for
 * a collection.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 * @version $Rev: 66 $ - $Date: 2010-09-06 15:29:07 +0000 (Mon, 06 Sep 2010) $
 */
public class CollectionResource extends AbstractFeedBaseResource {

    /**
     * The fragments modification time or <tt>-1</tt> if it's unknown.
     */
    private long _fragmentModification;

    /**
     * The snapshots modification time or <tt>-1</tt> if it's unknown.
     */
    private long _snapshotModification;

    /**
     * Initializes the instance.
     */
    public CollectionResource() {
        super();
    }

    /* (non-Javadoc)
     * @see org.restlet.resource.UniformResource#doInit()
     */
    @Override
    protected void doInit() throws ResourceException {
        super.initializeCollectionInfo();
        try {
            _fragmentModification = _storage.lastFragmentModification(_collInfo.getCollectionId());
            _snapshotModification = _storage.lastSnapshotModification(_collInfo.getCollectionId());
        }
        catch (StorageException ex) {
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, ex);
        }
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.server.impl.restlet.resources.AbstractFeedBaseResource#lastModification()
     */
    @Override
    protected long lastModification() {
        return Math.max(_fragmentModification, _snapshotModification);
    }

    /* (non-Javadoc)
     * @see org.restlet.resource.ServerResource#get(org.restlet.representation.Variant)
     */
    @Override
    public Representation get(Variant variant) throws ResourceException {
        final String iri = getRequestIRI();
        final String collTitle = _collInfo.getTitle();
        DefaultCollectionFeed feed = new DefaultCollectionFeed(iri, 
                                getConfiguration().getCollectionFeedTitle(collTitle),
                                _collInfo.getUpdated(), _collInfo.getDependencies());
        applyAuthorInfo(feed);
        feed.setSnapshotsEntry(new DefaultLinkAwareEntry(linkToSnapshots(),
                                getConfiguration().getSnapshotsEntryTitle(collTitle),
                                _snapshotModification));
        feed.setFragmentsEntry(new DefaultLinkAwareEntry(linkToFragments(), 
                getConfiguration().getFragmentsEntryTitle(collTitle),
                _fragmentModification));
        return new WritableAwareRepresentation(variant.getMediaType(), 
                new CollectionFeedWriter(feed, makeFeedHandler(variant)), 
                _collInfo.getUpdated());
    }

}
